package com.graduation.project.controller.system;

import com.graduation.project.entity.dto.role.SaveOrUpdateRoleDTO;
import com.graduation.project.entity.dto.role.ListRoleDTO;
import com.graduation.project.entity.dto.UpdateStatusDTO;
import com.graduation.project.service.system.RoleManagementService;
import com.graduation.project.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 卑微小峰
 * @date 2022/10/29
 * 角色管理控制类
 */
@RestController
@RequestMapping("/role_management")
public class RoleManagementController {

    @Autowired
    private RoleManagementService roleManagementService;

    /**
     * 新增角色
     * @param input 新增/修改角色输入类
     * @return ResultUtil
     */
    @PostMapping("/post/saveRole")
    public ResultUtil<?> saveRole(@RequestBody SaveOrUpdateRoleDTO input) {
        return roleManagementService.saveRole(input);
    }

    /**
     * 修改角色
     * @param input 新增/修改角色输入类
     * @return ResultUtil
     */
    @PutMapping("/put/updateRole")
    public ResultUtil<?> updateRole(@RequestBody SaveOrUpdateRoleDTO input) {
        return roleManagementService.updateRole(input);
    }

    /**
     * （批量）删除角色：逻辑删除
     * @param idList 选中数据项id集合
     * @return ResultUtil
     */
    @DeleteMapping("/delete/batchDeleteRoleById")
    public ResultUtil<?> batchDeleteRoleById(@RequestBody List<Long> idList) {
        return roleManagementService.batchDeleteRoleById(idList);
    }

    /**
     * 根据id修改状态
     * @param input 修改状态输入类
     * @return ResultUtil
     */
    @PutMapping("/put/updateRoleStatusById")
    public ResultUtil<?> updateRoleStatusById(@RequestBody UpdateStatusDTO input) {
        return roleManagementService.updateRoleStatusById(input);
    }

    /**
     * （条件）分页查询角色列表
     * @param input 条件查询输入类
     * @return ResultUtil
     */
    @PostMapping("/post/listRoleByCondition")
    public ResultUtil<?> listRoleByCondition(@RequestBody ListRoleDTO input) {
        return roleManagementService.listRoleByCondition(input);
    }

    /**
     * 查询菜单树
     * @return ResultUtil
     */
    @GetMapping("/get/listMenuTree")
    public ResultUtil<?> listMenuTree() {
        return roleManagementService.listMenuTree();
    }

    /**
     * 获取已拥有的权限Id集合
     * @param id 角色Id
     * @return ResultUtil
     */
    @GetMapping("/get/listMenuIdByRoleId/{id}")
    public ResultUtil<?> listMenuIdByRoleId(@PathVariable("id") Long id) {
        return roleManagementService.listMenuIdByRoleId(id);
    }

    /**
     * 权限分配
     * @param id 角色id
     * @param menuIds 菜单id集合
     * @return ResultUtil
     */
    @PostMapping("/post/menuAssignment/{id}")
    public ResultUtil<?> menuAssignment(@PathVariable("id") Long id, @RequestBody List<Long> menuIds) {
        return roleManagementService.menuAssignment(id, menuIds);
    }
}
